﻿
namespace schemas.microsoft.com.sharepoint {

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Schema;

    public partial class CamlQueryRoot {
        public CamlQueryRoot(string orderBy, bool ascending) : this() {
            if (!string.IsNullOrEmpty(orderBy)) {
                FieldRefDefinition orderField = new FieldRefDefinition(orderBy);
                orderField.Ascending = ascending.ToString().ToUpper();
                this.OrderBy.FieldRef.Add(orderField);
            }
        }
    }

    public partial class ValueDefinition {

        public ValueDefinition(string fieldType)
            : this()
        {
            this.Type = fieldType;
        }
        public ValueDefinition(string fieldType, string valueConstant)
            : this(fieldType)
        {
            this.SetValue(valueConstant, XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).Datatype);
        }

        //<Value Type=”DateTime” IncludeTimeValue=”TRUE”><Today /></Value>    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="includeTime"></param>
        public ValueDefinition(DateTime value, bool includeTime, bool convertToUtc)
            : this("DateTime")
        {
            this.SetAttribute(
                "IncludeTimeValue", 
                includeTime,
                XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Boolean).Datatype
            );
            this.SetValue(convertToUtc ? value.ToUniversalTime() : value, XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.DateTime).Datatype);
        }
    }

    public partial class FieldRefDefinition {
        public FieldRefDefinition(string fieldName)
            : this()
        {
            this.Name = fieldName;
            // TODO do we need to do anything special to indicate it is an internal fieldName name?
        }
    }

}
